package dao.impl;

import dao.IAddressDao;
import dao.IUserDao;
import jdbc_util.FirstApp;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import pojo.Address;
import pojo.User;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liang
 */
public class AddressDaoImpl implements IAddressDao {
    private IUserDao userDao = new UserDaoImpl();
    @Override
    public void addAddress(Address address, User user) {
        QueryRunner qr = new QueryRunner();
        Connection connection = null;
        try {
            connection = FirstApp.getConn();
            connection.setAutoCommit(false);
            String sql = "update user set password=? where userid =?";
            User user1 = userDao.findByid(user.getId());
            if (user1!=null){
                qr.update(connection, sql,address.getAddress(),user1.getUserInfo().getUserinfoid());
                connection.commit();
            }

        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                connection.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }finally {
            try {
                DbUtils.close(connection);
            } catch (Exception throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public void updateAddress(Address address, User user) {
        QueryRunner qr = new QueryRunner();
        Connection connection = null;
        try {
            connection = FirstApp.getConn();
            connection.setAutoCommit(false);
            String sql = "update address set address=? where userinfo =?";
            User user1 = userDao.findByid(user.getId());
            if (user1!=null){
                qr.update(connection, sql,address.getAddress(),user1.getUserInfo().getUserinfoid());
                connection.commit();
            }

        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                connection.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }finally {
            try {
                DbUtils.close(connection);
            } catch (Exception throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public void deleteAddress(User user) {
        QueryRunner qr = new QueryRunner();
        Connection connection = null;
        try {
            connection=FirstApp.getConn();
            connection.setAutoCommit(false);
            String sql = "delete from address where userinfo=?";
            User user1 = userDao.findByid(user.getId());
            if (user1!=null) {
                qr.update(connection, sql, user.getId());
                connection.commit();
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                connection.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }finally {
            try {
                DbUtils.close(connection);
            } catch (Exception throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public List<String> findAddressByUserInfoId(int id) {
        List<Object[]> address=null;
        List<String> address1=new ArrayList<String>();
        QueryRunner queryRunner=new QueryRunner();
        String sql="select address from address where userinfo=?";
        try {
            address=queryRunner.query(FirstApp.getConn(),sql,new ArrayListHandler(),id);
            for(int i=0;i<address.size();i++)
            {
                address1.add((String) address.get(i)[0]);
            }
            return address1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
